<?php
namespace app\crm\controller;
use think\Loader;
use think\Cache;

class Visittask extends CrmController
{
    /**
     * 坐席界面加载回访任务列表界面
     */
    public function visitTaskList()
    {	
		//分配增删改的权限
		$menuname = "Visit Task List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);
		$arrUsers = Loader::model('Users')->getUsersList();
        $this->assign("arrUsers",$arrUsers);
		// 模板输出
        return $this->fetch('visitTaskList');
    }

    /**
     * 获取回访任务列表数据
     */
	public function visitTaskDataList()
	{
        $arrData = Loader::model('Visittask')->getVisitTaskData();
        echo json_encode($arrData);
	}
	
	/**
     * 坐席界面加载回访任务内客户资料列表界面
     */
    public function customerList()
    {	
		$task_id = $_REQUEST["task_id"];
		//分配增删改的权限
		$menuname = "Visit Task List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];	
		$this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);
		$this->assign("task_id",$task_id);
		// 模板输出
        return $this->fetch('customerList');
    }
	
	/**
     * 获取回访任务内会员数据
     */
	public function customerDataList(){
		$arrData = Loader::model('Visittask')->customerDataList();
        echo json_encode($arrData);
	}
	
	/**
     * 渲染模板editCustomer
     */
    public function editCustomer()
    {
        $menuname = "Visit Task List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $customer_code = $_REQUEST['customer_code'];
		$task_id = $_REQUEST["task_id"];
		$this->assign("task_id",$task_id);
        if (empty($customer_code)) {
            echo "页面出现未知错误！";
            exit;
        }
        $where = 'b.customer_code="'.$customer_code.'"';
        // 基本资料
        $customerData = Loader::model('Customer')->getCustomerData($where);
        // echo $where;exit;
        $customerData = $customerData['data'][0];
        // echo "<pre>";
        // print_r($customerData);exit;
        if ($customerData['sex'] == "男") {
            $customerData['option'] = '<option value="1" selected>男</option> <option value="2">女</option>';
        } elseif ($customerData['sex'] == "女") {
            $customerData['option'] = '<option value="1">男</option> <option value="2" selected>女</option>';
        } else {
            $customerData['option'] = '<option value="1">男</option> <option value="2">女</option>';
        }
		
		$users = Cache::get('users');
        $customerData['createuser'] = $users[$customerData['create_user']]['cn_name']."（".$customerData['create_user']."）";

        $member_option = "";
        if ($customerData['member_level'] == "a类客户") {
            $member_option .= '<option value="1" selected>a类客户</option>';
        } else {
            $member_option .= '<option value="1">a类客户</option>';
        }

        if ($customerData['member_level'] == "b类客户") {
            $member_option .= '<option value="2" selected>b类客户</option>';
        } else {
            $member_option .= '<option value="2">b类客户</option>';
        }

        if ($customerData['member_level'] == "c类客户") {
            $member_option .= '<option value="3" selected>c类客户</option>';
        } else {
            $member_option .= '<option value="3">c类客户</option>';
        }

        if ($customerData['member_level'] == "d类客户") {
            $member_option .= '<option value="4" selected>d类客户</option>';
        } else {
            $member_option .= '<option value="4">d类客户</option>';
        }

        if ($customerData['member_level'] == "e类客户") {
            $member_option .= '<option value="5" selected>e类客户</option>';
        } else {
            $member_option .= '<option value="5">e类客户</option>';
        }

        if ($customerData['member_level'] == "不成单") {
            $member_option .= '<option value="6" selected>不成单</option>';
        } else {
            $member_option .= '<option value="6">不成单</option>';
        }

        $customerData['member_option'] = $member_option;

        /*echo "<pre>";
        print_r($customerData);exit;*/
        $this->assign('customerData', $customerData);

		$result = Cache::get('visitSelect');
        // 回访类型
        $visit_type = '<option value="">请选择</option>';
        foreach ($result['visit_type'] as $val) {
            $visit_type .= '<option value="'.$val['id'].'">'.$val['visit_type'].'</option>';
        }
        $this->assign('visit_type', $visit_type);
        // 回访结果
        $visit_result = '<option value="">请选择</option>';
        foreach ($result['visit_result'] as $val) {
            $visit_result .= '<option value="'.$val['id'].'">'.$val['result_name'].'</option>';
        }
        $this->assign('visit_result', $visit_result);
		
        $address = Loader::model('Customer')->getAddress($customer_code);
        $this->assign('address', $address);
        // 模板输出
        return $this->fetch('editCustomer');
    }
	
	 /**
     * 渲染模板visit
     */
    public function visit()
    {
        $menuname = "Customer List";
        $p_menuname = $_SESSION['menu'][$menuname]; //父菜单
        $priv = $_SESSION["user_priv"][$p_menuname][$menuname];     
        $this->assign("priv",$priv);
        $this->assign('customer_code', $_GET['customer_code']);
		$task_id = $_REQUEST["task_id"];
		$this->assign("task_id",$task_id);
        // 模板输出
        return $this->fetch('visit');
    }
	
	/**
     * 添加任务回访记录
     */
	public function inserTasktVisit()
	{
		$data = $_REQUEST;
        $data['create_user'] = $_SESSION['user_info']['username'];
        $data['create_time'] = date('Y-m-d H:i:s');
        // print_r($data);exit;
        Loader::model('Visittask')->inserTasktVisit($data);
		
	}
}

?>